﻿$('#startConsignedTm').datetimepicker({
    language: 'zh-CN',//显示中文
    format: 'yyyy-mm-dd',
    minView: 2,
    initialDate: new Date(),
    autoclose: true,//选中自动关闭
    todayBtn: true
});

$('#endConsignedTm').datetimepicker({
    language: 'zh-CN',
    format: 'yyyy-mm-dd',
    minView: 2,
    initialDate: new Date(),
    autoclose: true,
    todayBtn: true
});

var dt;
var directory = {};
function getdata() {
    var pam = {};
    pam.product_code = $(".Project option:selected").text();
    pam.process = $(".Process option:selected").text();
    pam.start = $('#startConsignedTm').val();
    pam.end = $('#endConsignedTm').val();
    $.ajax({
        type: "POST",
        url: "/Report/Getdata",
        data: pam,
        dataType: "json",
        success: function (data) {
            dt = data;
            PrimeYieldChart();
            platoChart();
            var table = "";
            var primeyield, finalyield;
            for (var i = 0; i < data.yield.length; i++) {
                if (data.yield[i].row_id && parseInt(data.yield[i].row_id) > 0) {
                    table += '<tr>'
                        + '<td class="tabletd" rowspan=' + data.yield[i].rowspan + '>' + data.yield[i].process_name + '</td>'
                        + '<td class="tabletd" rowspan=' + data.yield[i].rowspan + '>' + data.yield[i].input_qty + '</td>'
                        + '<td class="tabletd" rowspan=' + data.yield[i].rowspan + '>' + data.yield[i].output_qty + '</td>'
                        + '<td class="tabletd" rowspan=' + data.yield[i].rowspan + '>' + data.yield[i].prime_yield + '</td>'
                        + '<td class="tabletd" rowspan=' + data.yield[i].rowspan + '>' + data.yield[i].final_yield + '</td>'
                        + '<td>' + data.yield[i].category + '</td>'
                        + '<td class="tabletd">' + data.yield[i].prime_reject + '</td>'
                        + '<td class="tabletd">' + data.yield[i].retest_pass + '</td>'
                        + '<td class="tabletd">' + data.yield[i].prime_reject_yield + '</td>'
                        + '<td class="tabletd">' + data.yield[i].retest_pass_yield + '</td>'
                        + '</tr>';
                    if (!(primeyield)) {
                        primeyield = parseFloat(data.yield[i].prime_yield.substring(0, data.yield[i].prime_yield.indexOf("%"))) / 100;
                    }
                    else {
                        primeyield = primeyield * parseFloat(data.yield[i].prime_yield.substring(0, data.yield[i].prime_yield.indexOf("%"))) / 100;
                    }
                    if (!(finalyield)) {
                        finalyield = parseFloat(data.yield[i].final_yield.substring(0, data.yield[i].final_yield.indexOf("%"))) / 100;
                    }
                    else {
                        finalyield = finalyield * parseFloat(data.yield[i].final_yield.substring(0, data.yield[i].final_yield.indexOf("%"))) / 100;
                    }
                }
                else {
                    table += '<tr>'
                        + '<td>' + data.yield[i].category + '</td>'
                        + '<td class="tabletd">' + data.yield[i].prime_reject + '</td>'
                        + '<td class="tabletd">' + data.yield[i].retest_pass + '</td>'
                        + '<td class="tabletd">' + data.yield[i].prime_reject_yield + '</td>'
                        + '<td class="tabletd">' + data.yield[i].retest_pass_yield + '</td>'
                        + '</tr>';
                }
            }
            $('#yield_tbody').html(table);

            $('#primeyield').html(parseFloat((primeyield * 100).toFixed(2)) + "%");
            $('#finalyield').html(parseFloat((finalyield * 100).toFixed(2)) + "%");
        }
    });

};


$(function () {
    var projectsele = $(".Project");
    Projectdata(projectsele);
    function Projectdata(projectsele) {
        $.ajax({
            type: "GET",
            url: "/Report/YieldComboBoxPrdData",
            dataType: "json",
            success: function (data) {
                projectsele.empty();
                if (data.length <= 0) {
                    var option = "<option>暂无数据</option>";//没有数据
                    projectsele.append(option);
                    return;
                }
                projectsele.append("<option value='-1'>请选择...</option>");//执行到这里就是有数据，将数据添加到相对应的下拉框
                for (var i = 0; i < data.length; i++) {
                    var option = "<option value=" + data[i].value + ">" + data[i].text + "</option>";
                    projectsele.append(option);
                }
            },
            error: function (xhr) {
                alert("error" + xhr.status);
            }
        });
    }

    $(".Project").change(function () {
        var Processsele = $(".Process");
        GetProcessInfo("product_code=" + $(this).val(), Processsele);
    });
    $(".Process").change(function () {
        //alert("选择的是:" + $(".Project option:selected").text() + " " + $(".Process option:selected").text() + " " + $(".county option:selected").text())
    });

    function GetProcessInfo(searstr, Processselect) {
        let sendtourl = encodeURI("/Report/YieldComboBoxProData?" + searstr);
        $.ajax({
            type: "GET",
            url: sendtourl,
            dataType: "json",
            success: function (data) {
                Processselect.empty();
                if (data.length <= 0) {
                    var option = "<option>暂无数据</option>";
                    Processselect.append(option);
                    return;
                }
                Processselect.append("<option value='ALL'>ALL</option>");
                //执行到这里就是有数据，将数据添加到相对应的下拉框
                for (var i = 0; i < data.length; i++) {
                    var option = "<option value=" + data[i].value + ">" + data[i].text + "</option>";
                    Processselect.append(option);
                }
            },
            error: function (xhr) {
                alert("error" + xhr.status);
            }
        });
    }
});


function PrimeYieldChart() {
    var colorList = ['#338AF6', '#78C77D', '#F4D35F', '#435184', '#8C67DE', '#6BC8CA', '#0AAF9F', '#E89589', '#16A085', '#4A235A', '#C39BD3 ', '#F9E79F', '#BA4A00', '#ECF0F1', '#616A6B', '#EAF2F8', '#4A235A', '#3498DB'];
    var chartprimeyield = document.getElementById("PrimeYieldChart");
    var myChart = echarts.init(chartprimeyield);
    var machineitem = [];
    var itemvaule = [];
    var itemcolor = [];
    var linevaule = [];
    if (dt) {
        for (var i = 0; i < dt.yieldmachine.length; i++) {
            machineitem.push(dt.yieldmachine[i].tester);
            itemvaule.push(dt.yieldmachine[i].machine_prime_yield);
            itemcolor.push(colorList[parseInt(dt.yieldmachine[i].sort)]);
            linevaule.push(parseInt(dt.yieldmachine[i].prime_reject));

        }
    };
    var linemax = Math.max.apply(null, linevaule);
    option = {
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'
            }
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '5%',
            containLabel: true
        },
        xAxis: [
            {
                type: 'category',
                data: machineitem,
                axisTick: {
                    alignWithLabel: true
                },
                axisLabel: {
                    show: true,
                    interval: 0,
                    rotate: 45,
                    textStyle: {
                        color: 'white'
                    }
                }
            }
        ],
        yAxis: [
            {
                type: 'value',
                //name: 'PrimeYield',
                min: 0,
                max: 100,
                interval: 20,
                axisLabel: {
                    show: true,
                    formatter: '{value} %',
                    textStyle: {
                        color: 'white'
                    }
                }
            },
            {
                type: 'value',
                min: 0,
                max: 100,
                interval: 20,
                axisLabel: {
                    show: false,
                    formatter: '{value}',
                    textStyle: {
                        color: 'white'
                    }
                }
            }
        ],
        series: [
            {
                name: 'PrimeYield',
                type: 'bar',
                barWidth: '60%',
                itemStyle: {
                    normal: {
                        color: function (params) {
                            var colorList = itemcolor;
                            return colorList[params.dataIndex]
                        }
                    }
                },
                data: itemvaule,
                tooltip: {
                    valueFormatter: function (value) {
                        return value + ' %';
                    }
                }
            },
            {
                name: 'PrimeRejectQty',
                type: 'line',
                yAxisIndex: 1,
                itemStyle: {
                    color: '#FC8452',
                    Width: 3.5
                },
                lineStyle: {
                    normal: {
                        color: '#FC8452'
                    }
                },
                data: linevaule
            }
        ]
    };
    myChart.setOption(option);
}

function platoChart() {
    var chartDom = document.getElementById("plato");
    var myChart = echarts.init(chartDom);
    var item = [];
    var itemvaule = [];
    var linevaule = [];
    var lastvaule;
    if (dt) {
        for (var i = 0; i < dt.yieldsummary.length; i++) {
            item.push(dt.yieldsummary[i].category);
            itemvaule.push(dt.yieldsummary[i].prime_reject_yield);
            if (i == 0) {
                lastvaule = dt.yieldsummary[i].plato_vaule;
                linevaule.push(dt.yieldsummary[i].plato_vaule);
            } else {
                lastvaule = lastvaule + dt.yieldsummary[i].plato_vaule;
                linevaule.push(lastvaule.toFixed(2));
            }
        }
    };
    var itemvaulemax = Math.max.apply(null, itemvaule);
    option = {
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'cross',
                crossStyle: {
                    color: 'white'
                }
            }
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '5%',
            containLabel: true
        },
        xAxis: [
            {
                type: 'category',
                data: item,
                axisLabel: {
                    show: true,
                    interval: 0,
                    rotate: 45,
                    textStyle: {
                        color: 'white'
                    }
                },
                axisPointer: {
                    type: 'shadow'
                }
            }
        ],
        yAxis: [
            {
                type: 'value',
                //name: '工序',
                min: 0,
                max: Math.ceil(itemvaulemax),
                interval: Math.ceil(itemvaulemax) / 5,
                axisLabel: {
                    //show: true,
                    formatter: '{value} %',
                    textStyle: {
                        color: 'white'
                    }
                }
            },
            {
                type: 'value',
                //name: '折线',
                min: 0,
                max: 100,
                interval: 20,
                axisLabel: {
                    //show: true,
                    formatter: '{value} %',
                    textStyle: {
                        color: 'white'
                    }
                }
            }
        ],
        series: [
            {
                name: 'Failure Rate',
                type: 'bar',
                tooltip: {
                    valueFormatter: function (value) {
                        return value + ' %';
                    }
                },
                data: itemvaule
            },
            {
                name: 'Proportion',
                type: 'line',
                yAxisIndex: 1,
                tooltip: {
                    valueFormatter: function (value) {
                        return value + ' %';
                    }
                },
                data: linevaule
            }
        ]
    };
    myChart.setOption(option);
};
var platochart = echarts.init(document.getElementById('plato'));
var primeyieldchart = echarts.init(document.getElementById('PrimeYieldChart'));
window.onresize = function () {
    platochart.resize();
    primeyieldchart.resize();
}